﻿using Super_Management_Mayhem.Content.Spritesheet;
using Super_Management_Mayhem.Screens.Playing.AttackModel;
using Super_Management_Mayhem.Screens.Playing.ObjectInterfaces;
using Super_Management_Mayhem.Screens.Playing.Players;
using Super_Management_Mayhem.Screens.Playing.Players.Manager;
using Transformable_Engine_v2.Engine.Animation.PositionAnimation;
using Transformable_Engine_v2.Engine.Animation.RotationAnimation;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.GameObjects.Primitives;
using Transformable_Engine_v2.Engine.Graphics;
using Transformable_Engine_v2.Engine.Helper;
using Transformable_Engine_v2.Engine.Time;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Super_Management_Mayhem.Screens.Playing.Items
{
    public class ThrownShuriken : Container, IOrderable, IAttackable, IAttacker
    {
        static TE_Rectangle hitBounding = new TE_Rectangle(0, 0, 1, 1, TE_Rectangle.Alignment.CenterMiddle).ToGridBound();

        private BaseAttack _attack;

        float _currentSpeed;

        private Timer _hitTimer;

        public ThrownShuriken(TE_Vector2 direction, Team team)
        {
            _attack = new UnidirectionalAttack(BaseAttack.TargetTeam.EnemyTo, team, this, 6, 300, hitBounding, direction.ToUnit(), 15);

            _currentSpeed = 12;

            var item = new ImageContainer
            {
                Sprite = ItemSheet.GetSprite(ItemSheet.Item.Shuriken),
                Origin = TE_Rectangle.Alignment.CenterMiddle,
                Rotation = Randomizer.Double().TimesTwoPi()
            };
            AddAnimations(new PositionUniformLinearAnimation(0, direction.ToUnit() * _currentSpeed));
            item.AddAnimations(new RotationUniformLinearAnimation(0, 2));
            AddChildren(item);

            if (Playscreen.SHOW_COLLISION)
            {
                AddChildren(new RectanglePrimitive
                                {
                                    Shape = hitBounding.AsGridBoundRectangle(),
                                    FillColor = new Color(Color.Magenta, 100)
                                });
            }

            _hitTimer = new Timer(200, false, null);
            _hitTimer.Start();
            AddTimers(_hitTimer);

            Die(60000);
        }

        public override void FixedStepUpdate(System.TimeSpan gameTime)
        {
            _attack.Attack(AbsolutePosition);
            base.FixedStepUpdate(gameTime);
        }

        public float GetOrderingFactor()
        {
            return Position.Y;
        }

        public TE_Rectangle GetBoundingBoxCollision()
        {
            return hitBounding + AbsolutePosition;
        }

        public Container GetReference()
        {
            return this;
        }

        public void OnAttack()
        {
        }

        public bool Hit(TE_Vector2 direction, TE_Vector2 attackerPosition, int hurtTime, int damage)
        {
            if (!_hitTimer.IsStarted)
            {
                _hitTimer.Reset();

                _currentSpeed *= 1.25f;
                ClearAnimation<PositionUniformLinearAnimation>();
                AddAnimations(new PositionUniformLinearAnimation(0, direction.ToUnit() * _currentSpeed));

                _attack.RemoveTeam();
            }

            return true;
        }

        public Team GetTeam()
        {
            return null;
        }

        IAttacker IAttacker.GetReference()
        {
            return this;
        }
    }

    public class PickedUpShuriken : Container, IThrowable
    {
        public override void LoadContent()
        {
            var item = new ImageContainer {
                                              Sprite = ItemSheet.GetSprite(ItemSheet.Item.Shuriken),
                                              Origin = TE_Rectangle.Alignment.CenterMiddle,
                                              Rotation = Randomizer.Double().TimesTwoPi()
                                          };
            item.AddAnimations(new RotationUniformLinearAnimation(0, 1));
            AddChildren(item);

            base.LoadContent();
        }

        public void Throw(TE_Vector2 throwerPosition, TE_Vector2 itemPosition, Manager.Direction latestDirection, Team team)
        {
            switch (latestDirection)
            {
                case Manager.Direction.Up:
                    Playscreen.Map.AddItem(new ThrownShuriken(new TE_Vector2(0, -1), team) { Position = throwerPosition.Offset(0, -16) });
                    break;
                case Manager.Direction.Down:
                    Playscreen.Map.AddItem(new ThrownShuriken(new TE_Vector2(0, 1), team) { Position = throwerPosition.Offset(0, 0) });
                    break;
                case Manager.Direction.Left:
                    Playscreen.Map.AddItem(new ThrownShuriken(new TE_Vector2(-1, 0), team) { Position = throwerPosition.Offset(-16, -8) });
                    break;
                case Manager.Direction.Right:
                    Playscreen.Map.AddItem(new ThrownShuriken(new TE_Vector2(1, 0), team) { Position = throwerPosition.Offset(0, -8) });
                    break;
            }
        }
    }

    public class IdleShuriken : BasePickableItem
    {
        public override void LoadContent()
        {

            _item = new ImageContainer {
                                              Sprite = ItemSheet.GetSprite(ItemSheet.Item.Shuriken),
                                              Origin = TE_Rectangle.Alignment.CenterMiddle,
                                              PositionGridBound = new TE_Vector2(0.5f, 0.35f),
                                          };

            _item.AddAnimations(new RotationUniformLinearAnimation(0, 1));

            _outline = new ImageContainer {
                                              Sprite = ItemSheet.GetSprite(ItemSheet.ItemOutline.ShurikenOutline),
                                              Origin = TE_Rectangle.Alignment.CenterMiddle,
                                              PositionGridBound = new TE_Vector2(0.5f, 0.35f),
                                          };
            _outline.AddAnimations(new RotationUniformLinearAnimation(0, 1));

            AddChildren(_item, _outline);

            base.LoadContent();
        }

        protected override int ReappearTime
        {
            get
            {
                return 5000;
            }
        }

        protected override IThrowable GetPickupItem()
        {
            return new PickedUpShuriken();
        }
    }
}
